﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;

namespace HCFramework.Page.Control.HtmlControl
{
    public partial class UsrHtmlControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadContent();
            }
        }

        private void LoadContent()
        {
            Trang trang = C.TrangController.GetCurrentTrangOrInsert();
            HtmlModule htmlModule = C.HtmlModuleController.GetCurrentHtmlModuleOrInsert(trang,this.ID);
            litContent.Text = htmlModule.Content;
            RadEditor1.Content = htmlModule.Content;
            litEdit.Visible = C.UserController.IsAdmin();
        }

        protected void litEdit_Click(object sender, EventArgs e)
        {
            plEditor.Visible = true;
            litContent.Visible = false;
            litEdit.Visible = false;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            HCDataContext dc = new HCDataContext();
            Trang trang = C.TrangController.GetCurrentTrangOrInsert(dc);
            HtmlModule htmlModule = C.HtmlModuleController.GetCurrentHtmlModuleOrInsert(trang, this.ID, dc);
            htmlModule.Content = RadEditor1.Content;
            C.HtmlModuleController.Update(htmlModule, dc);
            litContent.Text = htmlModule.Content;
            plEditor.Visible = false;
            litContent.Visible = true;
            litEdit.Visible = C.UserController.IsAdmin();
        }
    }
}